package com.acknight.controller;


import cn.hutool.core.bean.BeanUtil;
import com.acknight.constant.SystemConstants;
import com.acknight.dto.LoginFormDTO;
import com.acknight.dto.Result;
import com.acknight.dto.UserDTO;
import com.acknight.entity.Blog;
import com.acknight.entity.User;
import com.acknight.entity.UserInfo;
import com.acknight.service.IBlogService;
import com.acknight.service.IUserInfoService;
import com.acknight.service.IUserService;
import com.acknight.utils.UserHolder;
import com.acknight.vo.UserInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IUserInfoService userInfoService;

    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm){
        // 实现登录功能
        return userService.login(loginForm);
    }

    @PostMapping("/register")
    public Result register(@RequestBody LoginFormDTO loginForm){
        // 实现注册功能
        return userService.register(loginForm);
    }

    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public Result logout(){
        return userService.logout();
    }

    @GetMapping("/me")
    public Result me(@RequestParam("userId") Integer userId){
        return userService.me(userId);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId){
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }

    @GetMapping("/{id}")
    public Result queryUserById(@PathVariable("id") Long userId){
        // 查询详情
        User user = userService.getById(userId);
        if (user == null) {
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        // 返回
        return Result.ok(userDTO);
    }

    @PostMapping("/infosave")
    public Result infoSave(@RequestBody UserInfoVo userInfoVo) {
        return userService.infoSave(userInfoVo);
    }
}
